/*
 * Created on 2005-3-22
 *
 * Copyright: Bluejay Networks Inc. 2005
 *
 * This software is the proprietary information of Bluejay Networks Inc. 
 * 
 * Use is subject to license terms.
 * 
 */
package com.ims.iaa.swf.button.action;

import com.ims.iaa.swf.ByteArrayUtil;

/**
 * @author ShengMengBai
 *
 * 
 */
public final class GetURLAction2 extends BaseAction{
    public static final int code = PUSHPARA;
    private final byte[] m_bytData;
    private final int m_iOffset;
    private String m_sURLVar = "";
    private boolean m_bIsVariable = false;
    public GetURLAction2(byte[] bytData, int iOffset) {
        m_bytData = bytData;
        m_iOffset = iOffset;
	    //System.out.println("find button tag: " + code + " Position: " + iOffset);
    }

    /*
     * because can't get the document of this type
     * I not sure the format of the action.
     * analyze it base on some the data.
     * 96 0B 00 00 63 6C 69 63 6B 74 68 72 75 00 1C 96 08 00 00 5F 62 6c 61 6E 6B
     *             c  l  i  c  k  t  h  r  u                    _  b  l  a  n  k
     * 96 08 00 00 61 6C 69 63 6B 31 00   1C    96 07 00 00 61 74 72 72 31
     *             a  l  i  n  k  1                    a  t  a  r  1
     * 1C is the action of get the var real value
     * 
     * ActionCode		UI8			Action = 0x96
     * Length			UI16		UrlVariable length
     * data type		UI8		    00 = String
     * ......
     */
    public boolean parse(){
        try{
	        int iStart = m_iOffset;
		    int iActionCode = m_bytData[iStart];
		    //System.out.println("ActionCode " + iActionCode + " Position: " + iStart);
		    if(iActionCode != code){
		        throw new RuntimeException("parse error in GetURLAction class, GetURLAction.code=" + code + " and real code=" + iActionCode);
		    }
		    iStart += 1;
		    int iLen = ByteArrayUtil.toShort(m_bytData, iStart);
		    //System.out.println("String len:  " + iLen + " Position: " + iStart);
		    
		    iStart += 2;
		    if(m_bytData[iStart] != DATATYPESTRING){
		        //the parameter of the push is not a String, we treat it as not a url
		        return false;
		    }
		    
		    //get the first parameter of the push action
		    String m_sParaString1 = ByteArrayUtil.toString(m_bytData, iStart + 1);
		    String m_sParaString2 = "";
	        m_bIsVariable = true;
		    if(m_sParaString1.length() + 2 < iLen){
		        m_bIsVariable = false;
		    }
		    
		    iStart += iLen;		    
		    
		    // the push action push a variable
		    if(m_bIsVariable && m_bytData[iStart] != GETVARIA){
		        return false;
		    }
		    
		    //it may push two parameters separatly( we don't need the second parameter)
		    if(m_bytData[iStart] == GETVARIA){
		        iStart += 1;	
		    }
		    if(m_bytData[iStart] == PUSHPARA){
		       iStart += 1;
		       iLen = ByteArrayUtil.toShort(m_bytData, iStart);
		       iStart += iLen + 2;
		    }
		    
		    // may be the second parameter is also a variable
		    if(m_bytData[iStart] == GETVARIA){
		        iStart += 1;
		    }
		    
		    //the parameter may be the url or url variable only if the getUrl2 action after push
		    if(m_bytData[iStart] == ACTION_GETURL2){
		        m_sURLVar = m_sParaString1;
		        return true;
		    }
		    
		    
		    
		    //System.out.println("URL Var:  " + m_sURLVar);
		    return false;
        }catch(Exception e){
            return false;
        }
    }
    public String getURLVar(){
        return m_sURLVar;
    }
    /* (non-Javadoc)
     * @see com.ims.iaa.swf.SwfObject#getCode()
     */
    public int getCode() {
        return code;
    }
    
    public boolean getIsVariable(){
        return m_bIsVariable;
    }

}
